<?php
	error_reporting(0);
	set_time_limit (0);
	include 'simplehtmldom/simple_html_dom.php';
	include_once(dirname(__FILE__).'/dbutil.php');
	function saveRecord($table,$record,$db){
    	if(empty($table))
    		return false;
    	if(!is_array($record) || empty($record))
    		return false;
    	//$db = new DBUtil();
    	//$this->initDB();
    	$r = array();
    	foreach($record as $key=>$item){
    		$r[$db->escape($key)] = $db->escape($item);
    	}
    	$cols = implode("," , array_keys($r));
    	$values = implode("','" , $r);
    	$sql = "insert into " . $db->escape($table) . " (" . $cols . ") values ('" . $values . "')";
    	return $db->query($sql) ? $db->getlastid() : false;
    }
	echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
	$pageNo = 1;
	for($pageNo = 1; $pageNo <= 18; $pageNo++){
		$listPage = file_get_html('http://localhost/test-tx2.php?pageNo='.$pageNo);
		$list = $listPage->find('div[class=textList]');
		if(!empty($list)){
			foreach($list as $listItem){
				//var_dump(mb_convert_encoding($listItem->innertext,'utf-8','gbk'));
				$tmp = $listItem->find('li[class=title]')[0]->find('h2')[0]->find('a');
				if(empty($tmp)){
					echo 1;exit;
					continue;
				}
				$name = trim($tmp[0]->innertext);
				$tmp = $listItem->find('li[class=address]');
				$adress = trim($tmp[0]->title);
				if(empty($adress)){
					$adress = $tmp[0]->find('a')[0]->innertext;
				}
				$record = array();
				$record['house_name'] = $name ;
				$record['address'] = $adress;
				//$record['city'] = 'fuzhou';
				//状态
				$record['status'] = 0;
				$recordList[] = $record;
				//break;
			}
			$db = new DBUtil();
			//var_dump($recordList);exit;
			foreach($recordList as $item){
				$h = $db->getOneRow("select id from t_house where house_name = '".$db->escape($item['house_name'])."' ");
				if(empty($h)){
					// 保存楼盘信息
					saveRecord('t_house',$item,$db);
				}
			}
			$db->close();
		}
		echo $pageNo.'<br/>';
	}
function replace_unicode_escape_sequence($match)  
{         
        return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');       
} 
  
/**
 * get访问url
 */
function https_get($url){
	$ch = curl_init();
	//设置选项，包括URL
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //不验证证书
	//执行并获取HTML文档内容
	$output = curl_exec($ch);
	//释放curl句柄
	curl_close($ch);
	//$result = json_decode($output);
	//$result->subscribe
	return $output;
}

function unescape($str) {
 $str = rawurldecode($str);
 preg_match_all("/(?:%u.{4})|.{4};|&#\d+;|.+/U",$str,$r);
 $ar = $r[0];
 #print_r($ar);
 foreach($ar as $k=>$v) {
 if(substr($v,0,2) == "%u")
 $ar[$k] = iconv("UCS-2","GB2312",pack("H4",substr($v,-4)));
 elseif(substr($v,0,3) == "")
 $ar[$k] = iconv("UCS-2","GB2312",pack("H4",substr($v,3,-1)));
 elseif(substr($v,0,2) == "&#") {
 echo substr($v,2,-1)."";
 $ar[$k] = iconv("UCS-2","GB2312",pack("n",substr($v,2,-1)));
 }
 }
 return join("",$ar);
}
?>